###### This script will bind daily temp info from 6 CSV's (XY's, BOM Daily, button daily max & min, HOBO daily max & min) into one dataframe

# Establish directory
in.dir = '/home1/99/jc152199/MicroclimateStatisticalDownscale/dailyspatialextracts/'
setwd(in.dir)

rawfiles = list.files(in.dir, pattern='.csv')

final = NULL

for (i in rawfiles)

	{
	
	t.csv = read.csv(i)
	
	final = rbind(final,t.csv)
	
	cat(i,'\n')
	
	}
	
in.dir = '/home1/99/jc152199/MicroclimateStatisticalDownscale/ToAnalyse/'
setwd(in.dir)

# Read in .csv file with days

micro = read.csv(paste(in.dir,'micro_min_max_static_xy.csv',sep=''),header=T)
micro=micro[which(micro$date<20090602),] # Get the rest of the tmax, tmin, and realrad files then delete this line and run again

### Final is still missing info from 3 days, write it out anyways

write.csv(final, file='/home1/99/jc152199/MicroclimateStatisticalDownscale/ToAnalyse/final.csv',row.names=F)

### Need to append rows for DEM, slope, and aspect

### Read in model data

model.data = read.csv('/home1/99/jc152199/MicroclimateStatisticalDownscale/ToAnalyse/final.csv',header=T)

### Read in ASCII files of DEM, slope, and aspect

library('SDMTools')

xy = cbind(unique(as.character(model.data$site), as.numeric(model.data$lat), as.numeric(model.data$long)))
xy[,2] = as.numeric(xy[,2])

dem.asc = read.asc('/home1/99/jc152199/MicroclimateStatisticalDownscale/250mASCII/DEM/ASCII/dem_WTplusbuffer_LatLong_WGS1984_250mres.asc')
slope.asc = read.asc('/home1/99/jc152199/MicroclimateStatisticalDownscale/250mASCII/Slope/slope_WTplusbuffer_LatLong_WGS1984_250mres.asc')
aspect.asc = read.asc('/home1/99/jc152199/MicroclimateStatisticalDownscale/250mASCII/Aspect/aspect_WTplusbuffer_LatLong_WGS1984_250mres.asc')

dem.extract =extract.data(cbind(xy[,2],xy[,3]),dem.asc)
slope.extract = extract.data(xy,slope.asc)
aspect.extract = extract.data(xy,aspect.asc)

xyplusspatdata = cbind(xy,dem.extract,slope.extract,aspect.extract)

model.data$dem = NA
model.data$slope = NA
model.data$aspect = NA

for (i in c(1:nrow(model.data)))

	{
	
	model.data$dem=model.data[which(model.data$lat %in% xy[,1] & model.data$long %in% xy[,2]),]










